import router from "@/router";
import { useUserStoreHook } from "@/stores/user";
import { ElMessage } from "element-plus";
import { getToken } from "./cookies";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

NProgress.configure({ showSpinner: false });

// 可以不登录进行处理
const whiteList = ["/login", "/", "/a1a2", "/a1a3"];

router.beforeEach(async (to, _from, next) => {

  NProgress.start();

  if (whiteList.indexOf(to.path) !== -1) {
    // 在免登录白名单，直接进入
    next();
  } else {
    const userStore = useUserStoreHook();
    const token = getToken();
    // 请求地址不在白名单时
    if (!token) {
      // 如果没有登陆
      ElMessage.error("请登录后进行操作");

      next({ path: _from.fullPath });
    } else {
      next();
    }
  }
});

router.afterEach((to) => {

  NProgress.done();
});
